package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.constant.ResultCodeConstant;
import com.example.dto.AnnouncementDTO;
import com.example.dto.AnnouncementQuery;
import com.example.entity.AnnouncementDO;
import com.example.exception.BusinessException;
import com.example.mapper.AnnouncementMapper;
import com.example.service.AnnouncementService;
import java.util.Date;
import java.util.List;
import javax.validation.constraints.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 宣传信息管理ServiceImpl
 * </p>
 * @author 34448
 */
@Service
public class AnnouncementServiceImpl implements AnnouncementService {

    @Autowired
    private AnnouncementMapper announcementMapper;

    @Override
    @Transactional
    public Boolean addAnnouncement(@NotNull AnnouncementDTO announcementDTO, Integer userId) {
        // 检查用户是否有发布权限
        if (!hasPublishPermission(userId)) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }

        AnnouncementDO announcementDO = new AnnouncementDO();
        BeanUtils.copyProperties(announcementDTO, announcementDO);
        announcementDO.setCreateBy(userId);
        announcementDO.setCreateTime(new Date());
        announcementDO.setUpdateTime(new Date());
        return announcementMapper.insert(announcementDO) > 0;
    }

    @Override
    @Transactional
    public Boolean updateAnnouncement(@NotNull AnnouncementDTO announcementDTO, Integer userId) {
        // 检查用户是否有修改权限
        if (!hasUpdatePermission(userId)) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }

        AnnouncementDO announcementDO = announcementMapper.selectById(announcementDTO.getAnnouncementId());
        if (announcementDO == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000002, ResultCodeConstant.CODE_000002_MSG);
        }

        BeanUtils.copyProperties(announcementDTO, announcementDO);
        announcementDO.setUpdateBy(userId);
        announcementDO.setUpdateTime(new Date());
        return announcementMapper.updateById(announcementDO) > 0;
    }

    @Override
    @Transactional
    public Boolean deleteAnnouncement(@NotNull Integer announcementId, Integer userId) {
        // 检查用户是否有删除权限
        if (!hasDeletePermission(userId)) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }

        AnnouncementDO announcementDO = announcementMapper.selectById(announcementId);
        if (announcementDO == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000002, ResultCodeConstant.CODE_000002_MSG);
        }

        return announcementMapper.deleteById(announcementId) > 0;
    }

    @Override
    public List<AnnouncementDO> queryAnnouncements(AnnouncementQuery announcementQuery) {
        QueryWrapper<AnnouncementDO> wrapper = Wrappers.query();
        if (announcementQuery.getAnnouncementId() != null) {
            wrapper.eq("announcement_id", announcementQuery.getAnnouncementId());
        }
        return announcementMapper.selectList(wrapper);
    }

    @Override
    @Transactional
    public Boolean importAnnouncements(@NotNull String filePath, Integer userId) {
        // 检查用户是否有导入权限
        if (!hasImportPermission(userId)) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }

        // TODO: 解析文件并保存宣传信息到数据库
        return true;
    }

    @Override
    public List<AnnouncementDO> exportAnnouncements(List<Integer> announcementIds, Integer userId) {
        // 检查用户是否有导出权限
        if (!hasExportPermission(userId)) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }

        QueryWrapper<AnnouncementDO> wrapper = Wrappers.query();
        if (announcementIds != null && !announcementIds.isEmpty()) {
            wrapper.in("announcement_id", announcementIds);
        }
        return announcementMapper.selectList(wrapper);
    }

    /**
     * 检查用户是否有发布权限
     *
     * @param userId 用户ID
     * @return
     */
    private Boolean hasPublishPermission(Integer userId) {
        // TODO: 实现权限检查逻辑
        return true;
    }

    /**
     * 检查用户是否有修改权限
     *
     * @param userId 用户ID
     * @return
     */
    private Boolean hasUpdatePermission(Integer userId) {
        // TODO: 实现权限检查逻辑
        return true;
    }

    /**
     * 检查用户是否有删除权限
     *
     * @param userId 用户ID
     * @return
     */
    private Boolean hasDeletePermission(Integer userId) {
        // TODO: 实现权限检查逻辑
        return true;
    }

    /**
     * 检查用户是否有导入权限
     *
     * @param userId 用户ID
     * @return
     */
    private Boolean hasImportPermission(Integer userId) {
        // TODO: 实现权限检查逻辑
        return true;
    }

    /**
     * 检查用户是否有导出权限
     *
     * @param userId 用户ID
     * @return
     */
    private Boolean hasExportPermission(Integer userId) {
        // TODO: 实现权限检查逻辑
        return true;
    }
}